package lwh.table;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.BASE64Encoder;

import lwh.utils.DBHelper;

public class AttPrjDevelopPlan extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String offset=new String(request.getParameter("offset").getBytes("ISO8859-1"),"UTF-8");
		String count=new String(request.getParameter("count").getBytes("ISO8859-1"),"UTF-8");
		String sql = "SELECT	* from att_prj_develop_plan  LIMIT ?,?";
		StringBuilder builder = new StringBuilder();
		Connection con = null;
		try {

			PreparedStatement stat = null;
			ResultSet rs = null;
			con = DBHelper.getConnection();
			stat = con.prepareStatement(sql);
			stat.setInt(1, Integer.parseInt(offset));
			stat.setInt(2, Integer.parseInt(count));
			rs = stat.executeQuery();
			builder.append("[");
			while (rs.next()) {
				builder.append("{");
				builder.append("prj_plan_id:\"").append(
						rs.getString("prj_plan_id")).append("\",");
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				byte[] b = new byte[1024];
				int i = 0;
				InputStream in = rs.getBinaryStream("prj_plan");
				if(in!=null){
					while ((i = in.read(b, 0, 1024)) > 0) {
						out.write(b, 0, i);
					}
					byte[] req = out.toByteArray();
					BASE64Encoder encoder = new sun.misc.BASE64Encoder();
					String str = encoder.encode(req);
					builder.append("prj_plan:\"").append(str).append("\"");
				}else{
					builder.append("prj_plan:\"").append("\"");
				}
				
				
				
				
				builder.append("},");
			}
			if (builder.toString().equals("[")) {

			} else {
				builder.deleteCharAt(builder.length() - 1);
			}
			builder.append("]");
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		request.setAttribute("json", builder.toString());
		request.getRequestDispatcher("/WEB-INF/page/departmentJson.jsp")
				.forward(request, response);
	}

}
